import { eqV1ControllerUpdate } from "@/services/auto/eq";
import { useRequest } from "ahooks";
import { message, Switch } from "antd";
import { BooleanNumber } from "common/utils/data";
import { useState } from "react";

export default function SwitchOpen({ data }: {
    data: API.EQResult;
}) {
    const [checked, setChecked] = useState<boolean>(!!data.open);

    const {
        runAsync,
        loading
    } = useRequest(eqV1ControllerUpdate, {
        manual: true,
    });

    return <Switch loading={loading} checked={checked} onChange={(checked) => {
        runAsync({ uuid: data.uuid, }, { open: checked ? BooleanNumber.true : BooleanNumber.false }).then(()=>{
            message.success('操作成功');
            setChecked(checked);
        });
    }} />
}